package org.novudu.presentation.actions;

import java.io.BufferedOutputStream;
import java.io.Console;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.progress.IProgressConstants;
import org.novudu.Configuration;
import org.novudu.builder.DistributionBuilder;
import org.novudu.builder.DistributionBuilderListener;
import org.novudu.presentation.NovuduEditor;
import org.novudu.presentation.NovuduEditorPlugin;

public class BuildDistributionsAction extends Action {
	
	
	public BuildDistributionsAction() {
		super("Build All", ImageDescriptor.createFromURL((URL) NovuduEditorPlugin.INSTANCE.getImage("full/obj16/Novudu.png")));
	}
	
	@Override
	public void run() {
		
		NovuduEditor editor = (NovuduEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		
		final Configuration configuration = (Configuration) editor.getEditingDomain().getResourceSet().getResources().get(0).getContents().get(0);
		
		//System.err.println(configuration);
		
		final DistributionBuilder builder = new DistributionBuilder();
		
		Job j = new Job("Build") {
			
			@Override
			protected IStatus run(final IProgressMonitor monitor) {
				
				setProperty(IProgressConstants.ICON_PROPERTY, ImageDescriptor.createFromURL((URL) NovuduEditorPlugin.INSTANCE.getImage("full/obj16/Novudu.png")));
				
				MessageConsole console = new MessageConsole("Build", null);
				
				ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{console});
				
				IOConsoleOutputStream outputStream = console.newOutputStream();
				outputStream.setActivateOnWrite(true);
				final PrintStream outputPrintStream = new PrintStream(outputStream);
				
				final IOConsoleOutputStream errorStream = console.newOutputStream();
				errorStream.setActivateOnWrite(true);
			
				final Display display = PlatformUI.getWorkbench().getDisplay();
				display.syncExec(new Runnable() {

					@Override
					public void run() {
						errorStream.setColor(display.getSystemColor(SWT.COLOR_RED));
					}
					
				});
				
				final PrintStream errorPrintStream = new PrintStream(errorStream);
				
				monitor.beginTask("Building distributions", 100);
				
				builder.addListener(new DistributionBuilderListener() {
					
					@Override
					public void updateStatus(String status, int worked) {
						outputPrintStream.println(status);
						monitor.subTask(status);
						monitor.worked(worked);
					}
					
					@Override
					public void printInfo(String info) {
						outputPrintStream.println(info);
					}
					
					@Override
					public void printError(String error) {
						errorPrintStream.print(error);
					}
				});
				
				try {
					builder.buildAll(configuration, monitor);
				} catch (Exception e) {
					errorPrintStream.println(e.getMessage());
				}
				
				/*
				setProperty(IProgressConstants.ICON_PROPERTY, ImageDescriptor.createFromURL((URL) NovuduEditorPlugin.INSTANCE.getImage("full/obj16/Novudu.png")));
				
				MessageConsole console = new MessageConsole("Build", null);
				
				ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{console});
				
				IOConsoleOutputStream ios = console.newOutputStream();
				ios.setActivateOnWrite(true);
				PrintStream s = new PrintStream(ios);
				
				System.err.println(monitor);
				monitor.beginTask("Installing", 100);
				for (int i=0;i<10;i++) {
					try {
						if (monitor.isCanceled()) return Status.CANCEL_STATUS;
						String message = "Installing #" + i;
						monitor.subTask(message);
						s.println(message);
						
						
						
						monitor.worked(10);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				*/
				return Status.OK_STATUS;
			}
			
			@Override
			public Object getAdapter(Class adapter) {
				System.err.println(adapter);
				return super.getAdapter(adapter);
			}
		};
		j.setUser(true);
		j.schedule();
	}
	
}
